package com.brsya.movie.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.brsya.base.base.BaseLayoutFragment;
import com.brsya.base.base.Contents;
import com.brsya.base.util.LoggerUtil;
import com.brsya.base.util.UiUtil;
import com.brsya.movie.BuildConfig;
import com.brsya.movie.R;
import com.brsya.movie.activity.AboutActivity;
import com.brsya.movie.activity.AgreementWebActivity;
import com.brsya.movie.activity.BookListActivity;
import com.brsya.movie.activity.HelpActivity;
import com.brsya.movie.activity.PlayHistoryActivity;
import com.brsya.movie.application.MyApplication;
import com.brsya.movie.util.AppUtil;
import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdDislike;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAdSdk;
import com.bytedance.sdk.openadsdk.TTNativeExpressAd;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMWeb;

import java.util.List;

/**
 * Created by Brsya
 * CreateDate: 2022/7/6  16:10
 * Description:
 * 个人中心Fragment
 */
public class MeFragment extends BaseLayoutFragment implements View.OnClickListener {


    private final String AD_KEY = "switch_ad_recommend";
    private final String CONTENT_KEY = "switch_content_recommend";

    private ConstraintLayout clMeBox;
    private LinearLayout llMeBook;
    private LinearLayout llMeHistory;
    private LinearLayout llMeShare;
    private LinearLayout llScoreBox;
    private LinearLayout llAgreementPrivateBox;
    private LinearLayout llAgreementUserBox;
    private LinearLayout llHelpBox;
    private LinearLayout llAboutBox;
    private RelativeLayout rlAdBox;
    private LinearLayout llComContentBox;
    private View vIconContent;
    private LinearLayout llComAdBox;
    private View vIconAd;


    public static MeFragment newInstance() {

        Bundle args = new Bundle();

        MeFragment fragment = new MeFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void initView() {
        setContentViewId(R.layout.fragment_me);


        clMeBox = findViewById(R.id.cl_me_box);
        llMeBook = findViewById(R.id.ll_me_book);
        llMeHistory = findViewById(R.id.ll_me_history);
        llMeShare = findViewById(R.id.ll_me_share);
        llScoreBox = findViewById(R.id.ll_score_box);
        llAgreementPrivateBox = findViewById(R.id.ll_agreement_private_box);
        llAgreementUserBox = findViewById(R.id.ll_agreement_user_box);
        llHelpBox = findViewById(R.id.ll_help_box);
        llAboutBox = findViewById(R.id.ll_about_box);
        rlAdBox = findViewById(R.id.rl_ad_box);
        llComContentBox = findViewById(R.id.ll_com_content_box);
        vIconContent = findViewById(R.id.v_icon_content);
        llComAdBox = findViewById(R.id.ll_com_ad_box);
        vIconAd = findViewById(R.id.v_icon_ad);
    }

    @Override
    protected void initData() {
        UiUtil.setStatusPadding(getContext(), clMeBox);

        llMeBook.setOnClickListener(this);
        llMeHistory.setOnClickListener(this);
        llMeShare.setOnClickListener(this);
        llScoreBox.setOnClickListener(this);
        llAgreementPrivateBox.setOnClickListener(this);
        llAgreementUserBox.setOnClickListener(this);
        llHelpBox.setOnClickListener(this);
        llAboutBox.setOnClickListener(this);
        llComContentBox.setOnClickListener(this);
        llComAdBox.setOnClickListener(this);


        if (!"0".equals(MyApplication.aCache.getAsString(AD_KEY))) {
            vIconAd.setBackgroundResource(R.mipmap.icon_switch_open);
        }else{
            vIconAd.setBackgroundResource(R.mipmap.icon_switch_close);
        }

        if (!"0".equals(MyApplication.aCache.getAsString(CONTENT_KEY))) {
            vIconContent.setBackgroundResource(R.mipmap.icon_switch_open);
        }else{
            vIconContent.setBackgroundResource(R.mipmap.icon_switch_close);
        }

        loadAd();
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.ll_me_book) {
            startActivity(new Intent(getActivity(), BookListActivity.class));
        } else if (id == R.id.ll_me_history) {
            startActivity(new Intent(getActivity(), PlayHistoryActivity.class));
        } else if (id == R.id.ll_me_share) {
            // 分享App
            shear();
        } else if (id == R.id.ll_score_box) {
            // 评分
            AppUtil.launchAppDetail(getContext(), BuildConfig.APPLICATION_ID, "");
        } else if (id == R.id.ll_agreement_private_box) {
            // 隐私协议
            Intent intent = new Intent(getActivity(), AgreementWebActivity.class);
            intent.putExtra(Contents.INTENT_H5_AGREEMENT, Contents.AGREEMENT_URL_PRIVATE);
            intent.putExtra(Contents.INTENT_TITLE_AGREEMENT, getString(R.string.agreement_title_private));
            startActivity(intent);
        } else if (id == R.id.ll_agreement_user_box) {
            // 用户协议
            Intent intent = new Intent(getActivity(), AgreementWebActivity.class);
            intent.putExtra(Contents.INTENT_H5_AGREEMENT, Contents.AGREEMENT_URL_USER);
            intent.putExtra(Contents.INTENT_TITLE_AGREEMENT, getString(R.string.agreement_title_user));
            startActivity(intent);
        } else if (id == R.id.ll_help_box) {
            // 帮助反馈
            startActivity(new Intent(getActivity(), HelpActivity.class));
        } else if (id == R.id.ll_about_box) {
            // 关于我们
            startActivity(new Intent(getActivity(), AboutActivity.class));
        } else if (id == R.id.ll_com_content_box) {
            // 个性化内容
            String openType;
            if ("0".equals(MyApplication.aCache.getAsString(CONTENT_KEY))) {
                vIconContent.setBackgroundResource(R.mipmap.icon_switch_open);
                openType = "1";
            }else{
                vIconContent.setBackgroundResource(R.mipmap.icon_switch_close);
                openType = "0";
            }
            MyApplication.aCache.put(CONTENT_KEY, openType);
        } else if (id == R.id.ll_com_ad_box) {
            // 个性化广告
            String openType;
            if ("0".equals(MyApplication.aCache.getAsString(AD_KEY))) {
                vIconAd.setBackgroundResource(R.mipmap.icon_switch_open);
                openType = "1";
            }else{
                openType = "0";
                vIconAd.setBackgroundResource(R.mipmap.icon_switch_close);
            }
            MyApplication.aCache.put(AD_KEY, openType);
        }
    }


    private void loadAd() {
        // 加载广告
        float width = getContext().getResources().getDisplayMetrics().xdpi;
        TTAdNative adNative = TTAdSdk.getAdManager().createAdNative(getActivity());

        AdSlot adSlot = new AdSlot.Builder()
                // 广告ID 948498437 948498604
                .setCodeId(Contents.AD_TT_SDK_SETTING_ID)
                .setSupportDeepLink(true)
                .setAdCount(1)
                // 期望模板广告view的size,单位dp
                .setExpressViewAcceptedSize(width, 0)
                .setAdloadSeq(1)
                .build();

        adNative.loadNativeExpressAd(adSlot, new TTAdNative.NativeExpressAdListener() {

            @Override
            public void onError(int i, String s) {
                LoggerUtil.i("穿山甲广告加载失败" + i + "--ss--" + s);
            }

            @Override
            public void onNativeExpressAdLoad(List<TTNativeExpressAd> list) {
                LoggerUtil.i("穿山甲广告加载成功");
                rlAdBox.removeAllViews();
                for (TTNativeExpressAd adBean : list) {
                    rlAdBox.addView(adBean.getExpressAdView());
                    adBean.render();

                    adBean.setDislikeCallback(getActivity(), new TTAdDislike.DislikeInteractionCallback() {
                        @Override
                        public void onShow() {
                            if (rlAdBox != null) {
                                rlAdBox.removeAllViews();
                                rlAdBox.setVisibility(View.GONE);
                            }
                        }

                        @Override
                        public void onSelected(int i, String s, boolean b) {

                        }

                        @Override
                        public void onCancel() {

                        }
                    });
                }
            }
        });


    }


    /**
     * 分享APP
     */
    private void shear() {
        //        UMImage umImage = new UMImage(this, )
        UMWeb web = new UMWeb("http://download.yuzhitong.net/");
        web.setTitle(getResources().getString(R.string.app_name));//标题
        //        web.setThumb(thumb);//缩略图
        web.setDescription("");//描述
        new ShareAction(getActivity())
                .withMedia(web)
                .setDisplayList(SHARE_MEDIA.SINA, SHARE_MEDIA.QQ, SHARE_MEDIA.WEIXIN)
                .setCallback(new UMShareListener() {
                    @Override
                    public void onStart(SHARE_MEDIA share_media) {
                        LoggerUtil.e("开始");
                    }

                    @Override
                    public void onResult(SHARE_MEDIA share_media) {
                        LoggerUtil.e("成功");
                    }

                    @Override
                    public void onError(SHARE_MEDIA share_media, Throwable throwable) {
                        LoggerUtil.e("错误");
                    }

                    @Override
                    public void onCancel(SHARE_MEDIA share_media) {
                        LoggerUtil.e("取消");
                    }
                })
                .open();
    }
}
